package com.test;

import java.util.Arrays;
import java.util.List;

public class ShellSort {
    public static void main(String[] args) {
        int N = 10;
        Integer[] arr = {40891, 75274, 40324, 27613, 97468, 1394, 79573, 80799, 42914, 82343};
        System.out.println(Arrays.toString(arr));
        long begin = System.currentTimeMillis();
        ShellSort.sort3(arr);
        long end = System.currentTimeMillis();
        System.out.println(end - begin);
        System.out.println(Arrays.toString(arr));
    }

    private static void sort(Comparable[] arr) {
        int j;
        int num = 0;
        for (int gap = arr.length / 2; gap > 0; gap /= 2) {
            num++;
            for (int i = gap; i < arr.length; i++) {
                num++;
                Comparable tmp = arr[i];
                for (j = i; j >= gap && tmp.compareTo(arr[j - gap]) < 0; j -= gap) {
                    num++;
                    arr[j] = arr[j - gap];
                }
                arr[j] = tmp;
            }
        }
        System.out.println(num);
    }

    private static void sort2(Comparable[] arr) {
        for (int gap = arr.length / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < arr.length; i++) {
                for (int j = i; j >= gap; j -= gap) {
                    if (arr[j].compareTo(arr[j - gap]) < 0) {
                        swap(arr, j, j - gap);
                    } else break;
                }
            }
        }
    }

    private static void sort3(Comparable[] arr) {
        for (int gap = arr.length / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < arr.length; i++) {
//                Comparable temp = arr[i];
                for (int j = i; j >= gap && arr[j].compareTo(arr[j - gap]) < 0; j -= gap) {
                    swap(arr,j,j-gap);
                }
            }
        }
    }

    private static void swap(Comparable[] arr, int i, int j) {

        Comparable t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }
}
